package com.bmps.springcloud.insuredmicroservice.service.trial.strategy;

import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.trail.TrialPersonModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.trial.TrialUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * description:
 * HMT055保费试算
 *
 * @author jackdaw
 * @date 2020-03-10 11:45
 */
public class Hmt055TrialStrategy implements TrialStrategy {
    private Logger logger = LoggerFactory.getLogger(Hmt055TrialStrategy.class);

    @Override
    public TrialPersonModel premiumTrialCalculationPrepare(ProductModel productModel, RequestModel requestModel, RedisService redisService) {
        TrialPersonModel trialPersonModel = new TrialPersonModel();
        String riskCode = productModel.getProdCode();
        InsurantModel insurant = getInsurantModel(productModel.getProdInsSeq(), requestModel);
        //险种
        trialPersonModel.setRiskCode(riskCode);
        //计划编码
        trialPersonModel.setPlanVersion(productModel.getPlanVersion());

        //份数赋值
        trialPersonModel.setMult(productModel.getMult());

        //职业类别
        trialPersonModel.setJob(insurant.getOccupationType());
        StringBuilder sb = new StringBuilder();
        //查询产品部分要素参数值
        sb.append(AcceptConstant.REDIS_PREFIX_R004).append("|").append(riskCode);
        Object object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        TrialUtil.setSpecialElement(trialPersonModel, object.toString());
        sb.setLength(0);
        //查询产品保费计算公式
        sb.append(AcceptConstant.REDIS_PREFIX_R006).append("|")
                .append(trialPersonModel.getRiskCode()).append("|G");
        logger.info("【" + riskCode + "】【公式】【KEY】" + sb.toString());
        object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        trialPersonModel.setFormula(object.toString());
        sb.setLength(0);
        //查询费率相关要素
        sb.append(AcceptConstant.REDIS_PREFIX_R001).append("|")
                .append(riskCode);
        logger.info("【" + riskCode + "】【产品计算要素配置】【KEY】" + sb.toString());
        object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        //查询费率
        String result = TrialUtil.getRiskRateKey(trialPersonModel, object.toString());
        logger.info("【" + riskCode + "】【产品费率】【KEY】" + result);
        object = redisService.get(result);
        if (object == null) {
            return new TrialPersonModel();
        }
        TrialUtil.setElementRate(trialPersonModel, object.toString());
        sb.setLength(0);
        //查询产品计算公式对应参数
        sb.append(AcceptConstant.REDIS_PREFIX_R005).append("|").append(riskCode).append("|G");
        logger.info("【" + riskCode + "】【产品计算公式要素配置】【KEY】" + sb.toString());
        object = redisService.get(sb.toString());
        if (object == null) {
            logger.info("【" + riskCode + "】【产品计算公式要素配置结果】" + object);
            return new TrialPersonModel();
        }
        logger.info("【" + riskCode + "】【产品计算公式要素配置结果】" + object);
        // 元素对应关系
        TrialUtil.setElementMapping(trialPersonModel, object.toString());

        return trialPersonModel;
    }

    private InsurantModel getInsurantModel(int insSqe, RequestModel requestModel) {
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == insSqe) {
                return insurantModel;
            }
        }
        return new InsurantModel();
    }

}
